package com.bdcom.nms.NmsCollect.test.springtest;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.opengoss.snmphibernate.api.ISnmpSession;
import org.opengoss.snmphibernate.api.ISnmpSessionFactory;
import org.opengoss.snmphibernate.api.ISnmpTarget;
import org.opengoss.snmphibernate.api.ISnmpTargetFactory;
import org.opengoss.snmphibernate.api.SnmpAnnotationException;
import org.opengoss.snmphibernate.api.SnmpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bdcom.nms.NmsCollect.test.mib.Sample;


@Component
public class C {


	static  Logger _log=Logger.getLogger(C.class);
	
	@Autowired
	private ISnmpSessionFactory sessionFactory;

	@Autowired
	private ISnmpTargetFactory targetFactory;

	public String getSystemUpTime(){
		
		ISnmpSession session=null;
		
		String s=null;
		
		ISnmpTarget target=targetFactory.newSnmpTarget("172.16.21.24");
		
		_log.info("-----------------创建snmptarget");
		
		target.setCommunity("bdcom");
		
		target.setVersion(ISnmpTarget.V2C);
		
		target.setPort(161);
		
		try {
			 session=sessionFactory.newSnmpSession(target);
			 _log.info("-----------------创建snmpsession");	 
			 if(session==null){
				 _log.info("session为NULL");
				 return s;
			 }
			Sample sample= session.get(Sample.class);
			
			s= sample.getSysuptime();
		    _log.info("-----------------获取数据！");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			_log.error("--------------创建sessionIO异常！"+e.getLocalizedMessage());
		} catch (SnmpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			_log.error("--------------获取数据snmp异常！"+e.getLocalizedMessage());
		} catch (SnmpAnnotationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			_log.error("--------------获取数据注解异常！"+e.getLocalizedMessage());
		}finally{
			if(session!=null){
				try {
					session.close();
					_log.info("-----------------关闭session！");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					_log.error("--------------关闭session异常！"+e.getLocalizedMessage());
				}
			}
		}
		return s;
	}

}
